---
title: 'Table 3 Code'
author: "Xuqian Ma"
date: '2023-04-12'
output:
  html_document: default
  pdf_document: default
---


# TABLE 3A just for R2: Linear productivity, multi-way fixed effects, Merged Sample

```{r}
library(lmtest) # load package for clustered standard errors

# Regression for log_NumCall with fixed effects and clustered standard errors
model1 <- lm(log_NumCall ~ api + hotel + ltemp + ltemp2 + ym_* + dow_*, data = MergedSample)
cluster_se1 <- coeftest(model1, vcov. = vcovHC(model1, cluster = MergedSample$Date))

# Regression for log_NumCall with fixed effects, employee-level fixed effects and clustered standard errors
model2 <- lm(log_NumCall ~ api + ltemp + ltemp2 + ym_* + dow_*, data = MergedSample)
model2_fe <- plm(model2, index = c("EmployeeID", "Date"), model = "within", effect = "twoways")
cluster_se2 <- coeftest(model2_fe, vcov. = vcovHC(model2_fe, cluster = MergedSample$Date))

# Regression for log_minutes_logged with fixed effects and clustered standard errors
model3 <- lm(log_minutes_logged ~ api + hotel + ltemp + ltemp2 + ym_* + dow_*, data = MergedSample)
cluster_se3 <- coeftest(model3, vcov. = vcovHC(model3, cluster = MergedSample$Date))

# Regression for log_minutes_logged with fixed effects, employee-level fixed effects and clustered standard errors
model4 <- lm(log_minutes_logged ~ api + ltemp + ltemp2 + ym_* + dow_*, data = MergedSample)
model4_fe <- plm(model4, index = c("EmployeeID", "Date"), model = "within", effect = "twoways")
cluster_se4 <- coeftest(model4_fe, vcov. = vcovHC(model4_fe, cluster = MergedSample$Date))

# Table output for coefficients with api and temp in the variable names
tab <- list(cluster_se1, cluster_se2, cluster_se3, cluster_se4)
tabresults(tab, file = "", main = "", digits = 3, include.n = FALSE, summary.stats = FALSE, booktabs = TRUE, title = "TABLE 3A just for R2: Linear productivity, multi-way fixed effects, Merged Sample", rows = list(grep("*api*|*temp*", names(tab[[1]])[2:length(names(tab[[1]]))], value = TRUE)))
```

# TABLE 3A: Linear productivity, multi-way fixed effects, Merged Sample

```{r}
library(cgmreg) # load package for clustered group-mean regression
library(lmtest) # load package for clustered standard errors

# Group-mean regression for log_NumCall with fixed effects and clustered standard errors
model1 <- cgmreg(log_NumCall ~ api + hotel + ltemp + ltemp2 + ym_* + dow_*, data = MergedSample, cluster = c("Date", "EmployeeID"))
cluster_se1 <- coeftest(model1, vcov. = vcovCGM(model1, cluster = "EmployeeID"))

# Group-mean regression for r_log_NumCall with fixed effects and clustered standard errors
model2 <- cgmreg(r_log_NumCall ~ r_api + r_ltemp + r_ltemp2 + r_ym_* + r_dow_*, data = MergedSample, cluster = c("Date", "EmployeeID"))
cluster_se2 <- coeftest(model2, vcov. = vcovCGM(model2, cluster = "EmployeeID"))

# Group-mean regression for log_minutes_logged with fixed effects and clustered standard errors
model3 <- cgmreg(log_minutes_logged ~ api + hotel + ltemp + ltemp2 + ym_* + dow_*, data = MergedSample, cluster = c("Date", "EmployeeID"))
cluster_se3 <- coeftest(model3, vcov. = vcovCGM(model3, cluster = "EmployeeID"))

# Group-mean regression for r_log_minutes_logged with fixed effects and clustered standard errors
model4 <- cgmreg(r_log_minutes_logged ~ r_api + r_ltemp + r_ltemp2 + r_ym_* + r_dow_*, data = MergedSample, cluster = c("Date", "EmployeeID"))
cluster_se4 <- coeftest(model4, vcov. = vcovCGM(model4, cluster = "EmployeeID"))

# Table output for coefficients with api and temp in the variable names
tab <- list(cluster_se1, cluster_se2, cluster_se3, cluster_se4)
tabresults(tab, file = "", main = "", digits = 3, include.n = FALSE, summary.stats = FALSE, booktabs = TRUE, title = "TABLE 3A: Linear productivity, multi-way fixed effects, Merged Sample", rows = list(grep("*api*|*temp*", names(tab[[1]])[2:length(names(tab[[1]]))], value = TRUE)))
```

# TABLE 3B just for R2: Non-Linear productivity, multi-way fixed effects, Merged Sample

```{r}
library(cgmreg) # load package for clustered group-mean regression
library(lmtest) # load package for clustered standard errors

# Group-mean regression for log_NumCall with fixed effects and clustered standard errors
model1 <- cgmreg(log_NumCall ~ api_5_10 + api_10_15 + api_15_20 + api_20_p + hotel + ltemp + ltemp2 + ym_* + dow_*, data = MergedSample, cluster = c("Date", "EmployeeID"))
cluster_se1 <- coeftest(model1, vcov. = vcovCGM(model1, cluster = "EmployeeID"))

# Regression for log_NumCall with fixed effects, employee-level fixed effects and clustered standard errors
model2 <- lm(log_NumCall ~ api_5_10 + api_10_15 + api_15_20 + api_20_p + ltemp + ltemp2 + ym_* + dow_*, data = MergedSample)
model2_fe <- plm(model2, index = c("EmployeeID", "Date"), model = "within", effect = "twoways")
cluster_se2 <- coeftest(model2_fe, vcov. = vcovHC(model2_fe, cluster = MergedSample$Date))

# Group-mean regression for log_minutes_logged with fixed effects and clustered standard errors
model3 <- cgmreg(log_minutes_logged ~ api_5_10 + api_10_15 + api_15_20 + api_20_p + hotel + ltemp + ltemp2 + ym_* + dow_*, data = MergedSample, cluster = c("Date", "EmployeeID"))
cluster_se3 <- coeftest(model3, vcov. = vcovCGM(model3, cluster = "EmployeeID"))

# Regression for log_minutes_logged with fixed effects, employee-level fixed effects and clustered standard errors
model4 <- lm(log_minutes_logged ~ api_5_10 + api_10_15 + api_15_20 + api_20_p + ltemp + ltemp2 + ym_* + dow_*, data = MergedSample)
model4_fe <- plm(model4, index = c("EmployeeID", "Date"), model = "within", effect = "twoways")
cluster_se4 <- coeftest(model4_fe, vcov. = vcovHC(model4_fe, cluster = MergedSample$Date))

# Table output for coefficients with api and temp in the variable names
tab <- list(cluster_se1, cluster_se2, cluster_se3, cluster_se4)
tabresults(tab, file = "", main = "", digits = 3, include.n = FALSE, summary.stats = FALSE, booktabs = TRUE, title = "TABLE 3A: Linear productivity, multi-way fixed effects, Merged Sample", rows = list(grep("*api*|*temp*", names(tab[[1]])[2:length(names(tab[[1]]))], value = TRUE)))
```

# TABLE 3B: Non-Linear productivity, multi-way fixed effects, Merged Sample

```{r}
library(cgmreg) # load package for clustered group-mean regression
library(lmtest) # load package for clustered standard errors

# Group-mean regression for log_NumCall with fixed effects and clustered standard errors
model1 <- cgmreg(log_NumCall ~ api_5_10 + api_10_15 + api_15_20 + api_20_p + hotel + ltemp + ltemp2 + ym_* + dow_*, data = MergedSample, cluster = c("Date", "EmployeeID"))
cluster_se1 <- coeftest(model1, vcov. = vcovCGM(model1, cluster = "EmployeeID"))

# Group-mean regression for r_log_NumCall with fixed effects and clustered standard errors
model2 <- cgmreg(r_log_NumCall ~ r_api_5_10 + r_api_10_15 + r_api_15_20 + r_api_20_p + r_ltemp + r_ltemp2 + r_ym_* + r_dow_*, data = MergedSample, cluster = c("Date", "EmployeeID"))
cluster_se2 <- coeftest(model2, vcov. = vcovCGM(model2, cluster = "EmployeeID"))

# Group-mean regression for log_minutes_logged with fixed effects and clustered standard errors
model3 <- cgmreg(log_minutes_logged ~ api_5_10 + api_10_15 + api_15_20 + api_20_p + hotel + ltemp + ltemp2 + ym_* + dow_*, data = MergedSample, cluster = c("Date", "EmployeeID"))
cluster_se3 <- coeftest(model3, vcov. = vcovCGM(model3, cluster = "EmployeeID"))

# Group-mean regression for r_log_minutes_logged with fixed effects and clustered standard errors
model4 <- cgmreg(r_log_minutes_logged ~ r_api_5_10 + r_api_10_15 + r_api_15_20 + r_api_20_p + r_ltemp + r_ltemp2 + r_ym_* + r_dow_*, data = MergedSample, cluster = c("Date", "EmployeeID"))
cluster_se4 <- coeftest(model4, vcov. = vcovCGM(model4, cluster = "EmployeeID"))

# Table output for coefficients with api and temp in the variable names
tab <- list(cluster_se1, cluster_se2, cluster_se3, cluster_se4)
tabresults(tab, file = "", main = "", digits = 3, include.n = FALSE, summary.stats = FALSE, booktabs = TRUE, title = "TABLE 3A: Linear productivity, multi-way fixed effects, Merged Sample", rows = list(grep("*api*|*temp*", names(tab[[1]])[2:length(names(tab[[1]]))], value = TRUE)))
```
